package com.cardgame.dao;

import com.cardgame.model.card.*;
import org.springframework.stereotype.Repository;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.NamedType;

@Repository
public class CardDao implements ICardDao {
    private static final String CARD_DATA_PATH = "data/cards.json";
    private final Map<Integer, Card> cardCache = new HashMap<>();
    private final ObjectMapper objectMapper = new ObjectMapper();

    public CardDao() {
        configureObjectMapper();
        loadCards();
    }

    // 注册子类以支持反序列化
    private void configureObjectMapper() {
        objectMapper.registerSubtypes(
                new NamedType(RoleCard.class, "role"),
                new NamedType(ActionCard.class, "action"),
                new NamedType(TotalWeaponCard.class, "total_weapon"),
                new NamedType(RoleWeaponCard.class, "role_weapon"));
    }

    private void loadCards() {
        try {
            File file = new File(CARD_DATA_PATH);
            if (!file.exists()) {
                createDefaultCardData();
            } else {
                System.out.println("[卡牌库] 加载卡牌数据中...");
                Card[] cards = objectMapper.readValue(file, Card[].class);
                for (Card card : cards) {
                    cardCache.put(card.getId(), card);
                }
                System.out.println("[卡牌库] 成功加载 " + cardCache.size() + " 张卡牌");
            }
        } catch (Exception e) {
            System.err.println("[卡牌库] 加载失败，使用默认卡牌集");
            e.printStackTrace();
            // createDefaultCardData(); // 回退使用默认数据
        }
    }

    private void createDefaultCardData() {
        try {
            File file = new File(CARD_DATA_PATH);
            file.getParentFile().mkdirs();

            List<Card> cards = new ArrayList<>();
            int id = 1;
            // 创建角色卡
            // 战士
            int warriorLevel = 1;
            for (;warriorLevel <= 10; warriorLevel++) {
                RoleCard warrior = new RoleCard(
                        id,
                        warriorLevel + "级战士",
                        "images/战士立绘.png",
                        "一个强壮的"+ warriorLevel +"级战士",
                        50 * warriorLevel,
                        warriorLevel,
                        warriorLevel,
                        10 * warriorLevel);
                id++;
                cards.add(warrior);
            }
            // 法师
            int mageLevel = 1;
            for (;mageLevel <= 10; mageLevel++) {
                RoleCard mage = new RoleCard(
                        id,
                        mageLevel + "级法师",
                        "images/法师立绘.png",
                        "一个强大的"+ mageLevel +"级法师",
                        50 * mageLevel,
                        mageLevel,
                        2 * mageLevel,
                        5 * mageLevel);
                id++;
                cards.add(mage);
            }
            // 弓箭手
            int archerLevel = 1;
            for (;archerLevel <= 10; archerLevel++) {
                RoleCard archer = new RoleCard(
                        id,
                        archerLevel + "级弓箭手",
                        "images/弓箭手立绘.png",
                        "一个敏捷的"+ archerLevel +"级弓箭手",
                        50 * archerLevel,
                        archerLevel,
                        5 * archerLevel,
                        3 * archerLevel);
                id++;
                cards.add(archer);
            }

            // 创建行动卡
            // 加最大蓝量
            int addMaxManaLevel = 1;
            for (;addMaxManaLevel <= 10; addMaxManaLevel++) {
                ActionCard addMaxMana = new ActionCard(
                        id,
                        addMaxManaLevel + "级加最大蓝量",
                        "images/法师卡.png",
                        "增加最大蓝量。",
                        50 * addMaxManaLevel,
                        0,
                        0,
                        addMaxManaLevel,
                        0,
                        0,
                        -1,
                        addMaxManaLevel);
                id++;
                cards.add(addMaxMana);
            }
            // 治疗
            int healLevel = 1;
            for (;healLevel <= 10; healLevel++) {
                ActionCard heal = new ActionCard(
                        id,
                        healLevel + "级治疗",
                        "images/法师卡.png",
                        "恢复生命值。",
                        50 * healLevel,
                        10 * healLevel,
                        0,
                        0,
                        0,
                        0,
                        -1,
                        healLevel);
                id++;
                cards.add(heal);
            }
            // 加蓝
            int addManaLevel = 1;
            for (;addManaLevel <= 10; addManaLevel++) {
                ActionCard addMana = new ActionCard(
                        id,
                        addManaLevel + "级加蓝",
                        "images/法师卡.png",
                        "增加蓝量。",
                        50 * addManaLevel,
                        0,
                        2 * addManaLevel,
                        0,
                        0,
                        0,
                        -1,
                        addManaLevel);
                id++;
                cards.add(addMana);
            }

            // 创建全场装备卡
            // 增加最大血量
            int addMaxHpLevel = 1;
            for (;addMaxHpLevel <= 10; addMaxHpLevel++) {
                TotalWeaponCard addMaxHp = new TotalWeaponCard(
                        id,
                        addMaxHpLevel + "级增加最大血量",
                        "images/战士卡.png",
                        "增加最大血量。",
                        50 * addMaxHpLevel,
                        addMaxHpLevel,
                        addMaxHpLevel);
                id++;
                cards.add(addMaxHp);
            }

            // 创建角色装备卡
            // 战士剑
            int warriorSwordLevel = 1;
            for (;warriorSwordLevel <= 10; warriorSwordLevel++) {
                RoleWeaponCard warriorSword = new RoleWeaponCard(
                        id,
                        warriorSwordLevel + "级战士剑",
                        "images/战士卡.png",
                        "战士的武器。",
                        50 * warriorSwordLevel,
                        warriorSwordLevel,
                        warriorSwordLevel * 2,
                        warriorSwordLevel * 2);
                id++;
                cards.add(warriorSword);
            }
            // 法师法杖
            int mageStaffLevel = 1;
            for (;mageStaffLevel <= 10; mageStaffLevel++) {
                RoleWeaponCard mageStaff = new RoleWeaponCard(
                        id,
                        mageStaffLevel + "级法师法杖",
                        "images/法师卡.png",
                        "法师的武器。",
                        50 * mageStaffLevel,
                        mageStaffLevel,
                        mageStaffLevel,
                        mageStaffLevel * 3);
                id++;
                cards.add(mageStaff);
            }
            // 弓箭手弓
            int archerBowLevel = 1;
            for (;archerBowLevel <= 10; archerBowLevel++) {
                RoleWeaponCard archerBow = new RoleWeaponCard(
                        id,
                        archerBowLevel + "级弓箭手弓",
                        "images/弓箭手卡.png",
                        "弓箭手的武器。",
                        50 * archerBowLevel,
                        archerBowLevel,
                        0,
                        archerBowLevel * 3);
                id++;
                cards.add(archerBow);
            }

            // 创建buff卡
            // 加血buff
            ActionCard addHPBuff = new ActionCard(
                    id,
                    "加血buff",
                    "images/战士卡.png",
                    "持续加血。",
                    50,
                    0, 0, 0, 0, 0,
                    2,
                    1);
            id++;
            cards.add(addHPBuff);
            // 加攻击力buff
            ActionCard addAttackBuff = new ActionCard(
                    id,
                    "加攻击力buff",
                    "images/战士卡.png",
                    "持续加攻击力。",
                    50,
                    0, 0, 0, 0, 0,
                    1,
                    1);
            id++;
            cards.add(addAttackBuff);

            // 创建deBuff卡
            // 减血deBuff
            ActionCard reduceHPDeBuff = new ActionCard(
                    id,
                    "减血deBuff",
                    "images/战士卡.png",
                    "持续减血。",
                    50,
                    0, 0, 0, 0, 0,
                    4,
                    1);
            id++;
            cards.add(reduceHPDeBuff);
            // 减攻击力deBuff
            ActionCard reduceAttackDeBuff = new ActionCard(
                    id,
                    "减攻击力buff",
                    "images/战士卡.png",
                    "持续减攻击力。",
                    50,
                    0, 0, 0, 0, 0,
                    3,
                    1);
            id++;
            cards.add(reduceAttackDeBuff);


            // 添加到缓存
            for (Card card : cards) {
                cardCache.put(card.getId(), card);
            }

            // 保存到文件
            List<Card> cardList = new ArrayList<>(cardCache.values());
            objectMapper.writeValue(file, cardList);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Card getCardById(Integer id) {
        return cardCache.get(id);
    }

    public List<Card> getAllCards() {
        return new ArrayList<>(cardCache.values());
    }

    public List<Card> getCardsByType(String type) {
        return cardCache.values().stream()
                .filter(card -> card.getType().equals(type))
                .collect(Collectors.toList());
    }
}